HA$PBExportHeader$u_cst_docuvta_factura_gr.sru
forward
global type u_cst_docuvta_factura_gr from u_cst_mantocabedet
end type
end forward

global type u_cst_docuvta_factura_gr from u_cst_mantocabedet
end type
global u_cst_docuvta_factura_gr u_cst_docuvta_factura_gr

type variables
long il_documento 
boolean ib_printGrabar=false
end variables

forward prototypes
public subroutine of_retrieve (long al_codigo)
public function boolean of_verificar_datos_detalle ()
public subroutine of_buscar_dato (string as_campo)
end prototypes

public subroutine of_retrieve (long al_codigo);dw_1.retrieve(al_codigo);
dw_3.event ue_dropdowndw()
dw_3.retrieve(al_codigo); commit;
end subroutine

public function boolean of_verificar_datos_detalle ();double ld_nstock,ld_cant,ld_precioventa
long ll_existe,ll_correlativo,ll_docu,ll_error, ll_return,ll_moneda
String ls_item,ls_correlativo,ls_stat,ls_serie,ls_error,ls_return,ls_msjError
integer li_fila,li_alma,li_item,li_lista

ls_correlativo = dw_1.object.tdocumento[1]
li_lista = dw_1.object.idlista[1]
ll_moneda = dw_1.object.idmoneda[1]
if isnull(ls_correlativo) then ls_correlativo = ''
if len(ls_correlativo) < 14  then
	f_msj('El correlativo no es v$$HEX1$$e100$$ENDHEX$$lido, por favor verifique en el registro de correlativos de documentos',4); return False
end if

ll_correlativo = long(mid(ls_correlativo,5,10))
ls_serie = mid(ls_correlativo,2,3)
ll_existe = 0
sqlca.Usp_GetExistDocuVta(1,ls_correlativo,ll_existe)

if ll_existe > 0 then
	ls_stat=space(1)
	sqlca.Usp_GetDocuVtaStat(1,ls_correlativo,ls_stat)
	choose case ls_stat 
		case 'A'
			f_msj('El documento ya fue emitido y esta anulado',4)
		case 'E'
			f_msj('El n$$HEX1$$fa00$$ENDHEX$$mero de documento ya fue emitido, el sistema generara otro correlativo',4)
			ls_serie =  space(3)
			sqlca.Usp_GetSerieDocuVta(gs_iduser,6,ls_serie)
			if ls_serie = '' or isnull(ls_serie) then ls_serie = '000'
			ls_correlativo = space(14)
			ls_msjError = space(50)
			sqlca.Usp_GetCorrelativoDocuVTa(gs_iduser,ls_serie,6,ls_correlativo ,ll_error, ls_msjError)
			dw_1.object.tdocumento[1]=ls_correlativo
	end choose
	Return False
else
	ll_error = 0 ; ls_error = space(50)
	sqlca.Usp_GetExisteCorrelativoDocVta(ll_correlativo, 1, ls_serie,gs_iduser, ll_error, ls_error)
	if ll_error <> 0 then
		f_msj('No existe el correlativo en la posicion actual, por favor verifique...',4); return false
	end if
end if

For li_fila = 1 To dw_3.RowCount()
	li_item = dw_3.object.iditem[li_fila]
	li_alma = dw_3.object.idalma[li_fila]
	ld_cant = dw_3.object.ncant[li_fila]
	//-- Valores ceros y nulos
	If dw_3.GetItemDecimal(li_fila,'ncant') = 0 Or 	dw_3.GetItemDecimal(li_fila,'npreciobase') = 0 Then 
		MessageBox('Advertencia','Error, Cantidad Digitada no Puede Ser Cero',stopsign!)
		Return False
	end if
		
	//-- Actualiza el numero de oc
	//idw_detalle.SetItem(ll_fila,'cc_ordcom', idw_ficha.object.cc_ordcom[1])
	sqlca.usp_getstockmitemalma( li_item,li_alma,ld_nstock)
	if isnull(ld_nstock) then ld_nstock = 0
	if ld_cant > truncate(ld_nstock,3) then
		dw_3.object.nstock[li_fila] = truncate(ld_nstock,3)
		f_msg('El stock actual es menor a la cantidad de salida ',1); return false
	end if
	ld_precioventa =  dw_3.object.npreciobase[li_fila]
	sqlca.Usp_GetValidarPrecio(li_item, li_lista, ll_moneda,ld_precioventa, ll_return, ls_return)
	if ll_return = - 1  then
		f_msg(ls_return,1); return false
	end if
Next

Return True

end function

public subroutine of_buscar_dato (string as_campo);s_parm	lstr_parm
integer li_find
string ls_item
//If (as_campo <> 'cc_ccosto' And as_campo <> 'cc_analis' And as_campo <> 'cc_req' and as_campo <> 'cn_notped' ) And &
//	as_campo <> 'cc_ctznac' And as_campo <> 'cc_artic' And as_campo <> 'cc_servic' Then Return
//
//lstr_parm.string_parm[1] = idw_ficha.GetItemString(idw_ficha.GetRow(),'cc_locac')

Choose Case as_campo
	Case 'iditem_1'
//		ls_item = string(dw_3.object.iditem[dw_3.getrow()])
//		dw_3.getchild( 'iditem', dwc); 
//		
//		li_find = dwc.find("string(iditem)= '"+ls_item+"'",1,dwc.rowcount())
	    ls_item =   ''  //dwc.GetItemString(li_find, "tdesccorta")
		lstr_parm.string_parm[1] = ls_item
//		lstr_parm.string_parm[1] = string(dw_3.object.iditem[dw_3.getrow()])
		lstr_parm.integer_parm[2] = dw_1.object.idlista[dw_1.getrow()]//lista
		lstr_parm.integer_parm[3] = dw_1.object.idalma[dw_1.getrow()]//almacen
		//istr_parm.string_parm[1]
		OpenWithParm(w_pop_clientes,lstr_parm)
      	lstr_parm = Message.PowerObjectParm
		IF lstr_parm.char_parm[1] = '1' THEN
			dw_3.object.iditem [dw_3.getrow()] = lstr_parm.integer_parm[1]
			dw_3.object.npreciobase [dw_3.getrow()] = lstr_parm.double_parm[1]
			dw_3.object.nstock [dw_3.getrow()] = lstr_parm.double_parm[2]
			dw_3.object.nPorc1 [dw_3.getrow()] = lstr_parm.double_parm[3]
			dw_3.object.idunid [dw_3.getrow()] = lstr_parm.string_parm[10]
			return
		End If	
end choose
end subroutine

on u_cst_docuvta_factura_gr.create
int iCurrent
call super::create
end on

on u_cst_docuvta_factura_gr.destroy
call super::destroy
end on

event ue_nuevo;call super::ue_nuevo;string ls_correlativo,ls_msjError,ls_serie
long ll_movimiento,ll_error,ll_existe
datetime ldt_femision
double ld_tica


dw_1.getchild( 'idlista', dwc)
dwc.settransobject(sqlca);dwc.retrieve();dwc.setfilter('iduser="'+gs_iduser+'"');dwc.filter()
if dwc.rowcount() > 0  then dw_1.object.idlista [1] =  dwc.getItemNumber(1, "idlista")


ll_error = 0
ll_movimiento=0
ls_serie = ''
sqlca.Usp_GetIdTablaINT(is_tabla,ll_movimiento)
dw_1.object.iddocumento[1]=ll_movimiento
sqlca.Usp_GetSerieDocuVta(gs_iduser,1,ls_serie)
if ls_serie = '' or isnull(ls_serie) then ls_serie = '000'

ls_correlativo = space(14)
ls_msjError = space(50)
sqlca.Usp_GetCorrelativoDocuVTa(gs_iduser,ls_serie,1,ls_correlativo ,ll_error, ls_msjError)
sqlca.Usp_GetExistDocuVta(1,ls_correlativo,ll_existe)

ldt_femision =  datetime(date(today()),time('00:00:00'))
ld_tica = 0
sqlca.Usp_GetTipoCambio(ldt_femision,'V',ld_tica)
//ls_correlativo

//SqlCA.usp_getcodigodocvta(2,ls_correlativo)
dw_1.object.ntipocambio[dw_1.rowcount()] = ld_tica
dw_1.object.tDocumento[dw_1.rowcount()] = ls_correlativo
dw_1.object.femision     [dw_1.rowcount()] = today()
dw_1.object.idDocu       [dw_1.rowcount()] = 1
dw_1.object.iduser       [dw_1.rowcount()] = gs_iduser
dw_1.object.idcentro[dw_1.rowcount()]=istr_parm.integer_parm[1]
dw_1.object.idalma[dw_1.rowcount()]=istr_parm.integer_parm[2]

f_protegercolumna(80,dw_1,'idalma')
f_protegercolumna(10,dw_1,'idlista')
if ls_msjError <> '' then
	f_msj(ls_msjError,1); return
end if

if ll_existe > 0 then
	f_msj('El correlativo ya existe, por favor verifique en el registro de correlativos de documentos ',4); return
end if

end event

event ue_agregar;integer li_row,li_fila
string ls_criterio,ls_alma
s_parm 				lstr_parm
If f_findrequired(dw_1) = -1 Then Return 

lstr_parm.string_parm[1] = ''
lstr_parm.integer_parm[1] = 0 //item
lstr_parm.integer_parm[2] =dw_1.object.idlista[1] //lista precio
lstr_parm.integer_parm[3] =dw_1.object.idalma[1] //lista precio


//lstr_parm.string_parm[2] = idw_ficha.object.cc_analiscli[1]
//				lstr_parm.string_parm[3] = idw_ficha.object.cn_suc[1]
OpenWithParm(w_pop_clientes,lstr_parm)
lstr_parm = Message.PowerObjectParm
IF lstr_parm.char_parm[1] <> '1' THEN  return

li_row =  dw_3.rowcount() 
dw_3.Event ue_dropdowndw()	
li_fila = dw_3.InsertRow(0)

if li_row = 0 then
	dw_3.object.idindice [li_fila ]=	1
else
	dw_3.object.idindice [dw_3.rowcount()]=	dw_3.object.idindice [li_row]+1
end if
//dw_3.GetChild("iditem", dwc);dwc.reset()
//gdc_mitem1.RowsCopy(gdc_mitem1.GetRow(), gdc_mitem1.RowCount(), Primary!, dwc, 1, Primary!)

ls_alma = string(dw_1.object.idalma[1])
ls_criterio="string(idalma)= '"+ls_alma+"'"
f_filtrar_item('MITEMALMA',dw_3,dwc,dwc2,ls_criterio)
//f_msg(string( dwc.rowcount( )) ,1)

dw_3.object.iddocumento [li_fila]=dw_1.object.idDocumento[1]
//dw_3.object.iditem [li_fila]=1
dw_3.scrolltorow(li_fila)
dw_3.object.idalma [dw_3.rowcount()]=dw_1.object.idalma[1]
f_protegercolumna(0,dw_1,'idalma')
f_protegercolumna(0,dw_1,'idlista')

dw_3.object.iditem [dw_3.rowcount()] = lstr_parm.integer_parm[1]
dw_3.object.npreciobase [dw_3.rowcount()] = lstr_parm.double_parm[1]
dw_3.object.nstock [dw_3.rowcount()] = lstr_parm.double_parm[2]
dw_3.object.nPorc1 [dw_3.rowcount()] = lstr_parm.double_parm[3]
dw_3.object.idunid [dw_3.rowcount()] = lstr_parm.string_parm[10]

dw_3.object.idlista [dw_3.rowcount()] = dw_1.object.idlista[1]
dw_3.setrow(dw_3.rowcount())
dw_3.setcolumn('ncant')
dw_3.setfocus()





end event

event ue_grabar_previo;LONG ll_movimiento,ll_error
string ls_correlativo,ls_msjError,ls_serie
double ld_tica
integer li_fila
datetime ldt_femision
CHOOSE CASE UPPER(is_button)
	case 'NUEVO'
		ll_movimiento=0
		sqlca.Usp_GetIdTablaINT(is_tabla,ll_movimiento)
		dw_1.object.idDOCUMENTO  [1]=ll_movimiento
		il_documento=ll_movimiento

		ldt_femision =  datetime(date(today()),time('00:00:00'))
		ld_tica = 0
		sqlca.Usp_GetTipoCambio(ldt_femision,'V',ld_tica)
		dw_1.object.ntipocambio[dw_1.rowcount()] = ld_tica
		
		ls_correlativo = space(14)
		ls_msjError = space(50)
//		sqlca.Usp_GetSerieDocuVta(gs_iduser,ls_serie)
//		if ls_serie = '' or isnull(ls_serie) then ls_serie = '000'
//		
//		sqlca.Usp_GetCorrelativoDocuVTa(gs_iduser,ls_serie,2,ls_correlativo ,ll_error, ls_msjError)
//		dw_1.object.tdocumento  [1]=ls_correlativo
		
		
		//dw_1.object.tdocumento    [1] = of_getTDocumento()
		dw_1.object.fproceso         [1] =today()
		if ib_printGrabar then
			dw_1.object.bimpreso         [1]=1
		end if
		
		event ue_sumar()
		for li_fila =1 to dw_3.rowcount( )
				dw_3.object.idindice  [li_fila] = li_fila
				dw_3.object.idDOCUMENTO  [li_fila] = dw_1.object.idDOCUMENTO  [1]
				dw_3.object.idlista  [li_fila] = dw_1.object.idlista  [1]
				//dw_3.object.fproceso         [li_fila] =	dw_1.object.fproceso         [1] 
				//dw_3.object.idcentro          [li_fila] =	dw_1.object.idcentro         [1] 
				dw_3.object.idalma            [li_fila] =	dw_1.object.idalma         [1] 
				//dw_3.object.idtrans            [li_fila] =	dw_1.object.idtrans         [1] 
				//dw_3.object.idmoneda        [li_fila] =	dw_1.object.idMoneda         [1] 
				
		next
	case 'EDITAR'
		for li_fila =1 to dw_3.rowcount( )
		
				dw_3.object.idDOCUMENTO  [li_fila] = dw_1.object.idDOCUMENTO  [1]
				dw_3.object.idlista  [li_fila] = dw_1.object.idlista  [1]
//				dw_3.object.fproceso         [li_fila] =	dw_1.object.fproceso         [1] 
				//dw_3.object.idcentro          [li_fila] =	dw_1.object.idcentro         [1] 
				//dw_3.object.idalma            [li_fila] =dw_1.object.idalma         [1] 
//				dw_3.object.idtrans            [li_fila] =	dw_1.object.idtrans         [1] 
//				dw_3.object.idmoneda        [li_fila] =	dw_1.object.idMoneda         [1] 
		next
		
		//DELETE FROM MKARDEX WHERE IDMOVIDOCU = :il_codigo;
		
		
		
END CHOOSE
end event

event ue_setdwc;//choose case is_tabla
//	case 'MMOVIMIENTO'
dw_2.GetChild("idvend", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )
dw_2.GetChild("idcond", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )
dw_2.GetChild("idclie", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )
dw_2.GetChild("idalma", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )
//dw_2.GetChild("idcentro", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )
dw_2.GetChild("idlista", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )			
commit;
end event

event ue_grabar_pos;if ib_printgrabar then
	this.event ue_print()
end if
	
this.event ue_nuevo()
end event

event ue_grabar_otros;
long ll_documento,ll_posicion
string ls_serie,ls_correlativo
ll_documento = DW_1.OBJECT.iddocumento[1]
//If sqlca.Usp_AddMKARDEX_Venta(ll_documento) <> 0  Then
//	rollback USING SQLCA ;
//	f_msj('Error al insertar al kardex',1)
//	return FALSE
//End if
ls_correlativo = dw_1.object.tdocumento[1]
ls_serie = mid(ls_correlativo,2,3)
ll_posicion= long(mid(ls_correlativo,5,10))
If sqlca.Usp_SetCorrelativoDocuVTa( gs_iduser,ls_serie,1, ll_posicion) <> 0 then
	rollback USING SQLCA ;
	f_msj('Error al actualizar correlativo de documentos',1)
	return FALSE
end if

return TRUE


end event

event constructor;call super::constructor;//idw_detalle = dw_3
end event

event ue_cambio_opc;string ssas_button


choose case upper(as_button) 
	case 'NUEVO'
		f_proteger_dw( dw_3,0)
		uo_toolbarstrip.visible = TRUE
		uo_toolbarstrip.of_setvisible( 'Imprimir-Grabar', true)
		uo_toolbarstrip.of_setvisible( 'Imprimir',false)
		uo_toolbarstrip.of_setvisible( 'Editar', FALSE);
		uo_toolbarstrip.of_setvisible( 'Anular', FALSE)
		uo_toolbarstrip.of_setvisible( 'Grabar', TRUE)// ; uo_toolbarstrip.of_setenabled( 'Grabar', ab_ver)	
		uo_toolbarstrip.of_setvisible( 'Cancelar', TRUE)//;uo_toolbarstrip.of_setenabled( 'Cancelar', ab_ver)
		uo_toolbarstrip.of_setvisible( 'Agregar', TRUE)//; uo_toolbarstrip.of_setenabled( 'Agregar', lb_sw)
		uo_toolbarstrip.of_setvisible( 'Eliminar Fila', TRUE)//; uo_toolbarstrip.of_setenabled( 'Eliminar Fila', lb_sw)
		dw_1.visible = TRUE ; 		dw_3.visible = TRUE ; 	dw_2.visible =FALSE; 		dw_4.visible =FALSE
		dw_1.enabled = true ; dw_3.enabled=true
		event ue_settabtext( 'NUEVO')
	case 'EDITAR'
		f_proteger_dw( dw_3,1)
		uo_toolbarstrip.visible = TRUE
		dw_1.enabled = true ; dw_3.enabled=true
		dw_1.visible = true ; 		dw_3.visible = true ; 	dw_2.visible =false; 		dw_4.visible =false
		uo_toolbarstrip.of_setvisible( 'Imprimir', true)
		uo_toolbarstrip.of_setvisible( 'Editar', false);
		uo_toolbarstrip.of_setvisible( 'Anular', false)
		uo_toolbarstrip.of_setvisible( 'Grabar', true) //; uo_toolbarstrip.of_setenabled( 'Grabar', ab_ver)	
		uo_toolbarstrip.of_setvisible( 'Cancelar', true)//;uo_toolbarstrip.of_setenabled( 'Cancelar', ab_ver)
		uo_toolbarstrip.of_setvisible( 'Agregar', true)//; uo_toolbarstrip.of_setenabled( 'Agregar', ab_ver)
		uo_toolbarstrip.of_setvisible( 'Eliminar Fila', true)//; uo_toolbarstrip.of_setenabled( 'Eliminar Fila', ab_ver)
		event ue_settabtext( 'EDITAR')
	case 'ANULAR'
		f_proteger_dw( dw_3,1)
		uo_toolbarstrip.of_setvisible( 'Imprimir', false)
		uo_toolbarstrip.visible = TRUE
		dw_1.enabled = false ; dw_3.enabled=false
		dw_1.visible = true ; 		dw_3.visible = true ; 	dw_2.visible =false; 		dw_4.visible =false
		uo_toolbarstrip.of_setvisible( 'Editar', false);
		uo_toolbarstrip.of_setvisible( 'Anular', false)
		uo_toolbarstrip.of_setvisible( 'Grabar', true) //; uo_toolbarstrip.of_setenabled( 'Grabar', ab_ver)	
		uo_toolbarstrip.of_setvisible( 'Cancelar', true)//;uo_toolbarstrip.of_setenabled( 'Cancelar', ab_ver)
		uo_toolbarstrip.of_setvisible( 'Agregar', false)//; uo_toolbarstrip.of_setenabled( 'Agregar', ab_ver)
		uo_toolbarstrip.of_setvisible( 'Eliminar Fila', false)//; uo_toolbarstrip.of_setenabled( 'Eliminar Fila', ab_ver)
	case 'CANCELAR'	
		uo_toolbarstrip.of_setvisible( 'Imprimir-Grabar', false)
		dw_1.enabled = false ; dw_3.enabled=false
		uo_toolbarstrip.of_setvisible( 'Grabar', FALSE) //; uo_toolbarstrip.of_setenabled( 'Grabar', ab_ver)	
		dw_1.visible = FALSE ; 		dw_3.visible = FALSE ; 	dw_2.visible = TRUE; 		dw_4.visible =TRUE
		uo_toolbarstrip.of_setvisible( 'Cancelar', FALSE)
		uo_toolbarstrip.of_setvisible( 'Editar', false);
		uo_toolbarstrip.of_setvisible( 'Anular', TRUE)
		uo_toolbarstrip.of_setvisible( 'Agregar', FALSE)//; uo_toolbarstrip.of_setenabled( 'Agregar', lb_sw)
		uo_toolbarstrip.of_setvisible( 'Eliminar Fila', FALSE)//; uo_toolbarstrip.of_setenabled( 'Eliminar Fila', lb_sw)
		event ue_settabtext( 'Consultar')
	CASE 'CONSULTAR'
		uo_toolbarstrip.of_setvisible( 'Imprimir-Grabar', false)
		dw_1.enabled = false; dw_2.enabled = false
		uo_toolbarstrip.visible = TRUE
		uo_toolbarstrip.of_setvisible( 'Grabar', FALSE) //; uo_toolbarstrip.of_setenabled( 'Grabar', ab_ver)	
		dw_1.visible = FALSE ; 		dw_3.visible = FALSE ; 	dw_2.visible = TRUE; 		dw_4.visible =TRUE
		uo_toolbarstrip.of_setvisible( 'Imprimir', true)
		uo_toolbarstrip.of_setvisible( 'Cancelar', FALSE)
		uo_toolbarstrip.of_setvisible( 'Editar', false);
		uo_toolbarstrip.of_setvisible( 'Anular', true)
		
		uo_toolbarstrip.of_setvisible( 'Agregar', FALSE)//; uo_toolbarstrip.of_setenabled( 'Agregar', lb_sw)
		uo_toolbarstrip.of_setvisible( 'Eliminar Fila', FALSE)//; uo_toolbarstrip.of_setenabled( 'Eliminar Fila', lb_sw)
	CASE 'DEFAULT'
		dw_1.enabled= false ;	dw_1.visible = false ;		dw_2.visible = false ;		uo_1.visible = true
		uo_toolbarstrip.visible = false ;		st_titulo.visible = true ;		st_det1.visible = true
		event ue_settabtext( 'PRINCIPAL')
	case	else 
//		uo_toolbarstrip.of_setvisible( 'Grabar', lb_sw) //; uo_toolbarstrip.of_setenabled( 'Grabar', ab_ver)	
//		dw_1.visible = lb_sw ; 		dw_3.visible = lb_sw ; 	dw_2.visible = ab_ver; 		dw_4.visible =ab_ver
//		uo_toolbarstrip.of_setvisible( 'Cancelar', lb_sw)
//		uo_toolbarstrip.of_setvisible( 'Editar', ab_ver);
//		uo_toolbarstrip.of_setvisible( 'Anular', ab_ver)
//		uo_toolbarstrip.of_setvisible( 'Agregar', lb_sw)//; uo_toolbarstrip.of_setenabled( 'Agregar', lb_sw)
//		uo_toolbarstrip.of_setvisible( 'Eliminar Fila', lb_sw)//; uo_toolbarstrip.of_setenabled( 'Eliminar Fila', lb_sw)
end choose

end event

event ue_anular;integer li_documento
string ls_stat,ls_alma,ls_criterio
li_documento = dw_2.object.iddocumento[1]

select tstat into :ls_stat from vtadocu where iddocumento = :li_documento;

if ls_Stat = 'N' then 
	f_msg('El registro ya esta anulado',1)
	return
end if


iuo_tabmantomain.event ue_setbutton(is_button)
iuo_tabmantomain.event	ue_set_ib_sw(true)

iuo_grillamain.event ue_activa_opc( true)
event ue_settabtext ('Anular '+ is_entidad)
event ue_cambio_opc('ANULAR')
is_columna = dw_1.describe('#'+string(1)+'.name')
is_tipocolumna = dw_1.describe(is_columna+'.coltype')	

ls_alma = string(dw_2.object.idalma[1])
ls_criterio="string(idalma)= '"+ls_alma+"'"
f_filtrar_item('MITEMALMA',dw_3,dwc,dwc2,ls_criterio)

//dw_3.GetChild("iditem", dwc);dwc.reset()
//gdc_mitem1.RowsCopy(gdc_mitem1.GetRow(), gdc_mitem1.RowCount(), Primary!, dwc, 1, Primary!)

if left(is_tipocolumna,4)='char' then
	 is_codigo =dw_2.object.data[1,1]
	 of_retrieve( is_codigo)	
elseif is_tipocolumna='int' then
	 ii_codigo =dw_2.object.data[1,1]
	 of_retrieve( ii_codigo)	
else
	il_codigo =dw_2.object.data[1,1]
	 of_retrieve( il_codigo)	
end if		





end event

event ue_grabar_externo;long ll_movimiento = 0

ll_movimiento = dw_1.object.iddocumento[1]

//If sqlca.Usp_Anular_MKARDEX( 'V',ll_movimiento) <> 0  Then
//	rollback USING SQLCA ;
//	f_msj('Error al ANULAR al kardex',1)
//	return -1
//End if
of_retrieve(ll_movimiento)
dw_2.reset();dw_4.reset();
dw_1.RowsCopy(dw_1.GetRow(),dw_1.GetRow(), primary!, dw_2  , 1, Primary!)
dw_3.RowsCopy(1,dw_3.Rowcount(), primary!, dw_4, 1, Primary!)
dw_1.resetupdate();	dw_3.resetupdate(); 

COMMIT USING SQLCA;

iuo_grillamain.event dynamic ue_refrescar_row2(dw_2,dw_4,ii_row,is_button)
MessageBox('Informaci$$HEX1$$f300$$ENDHEX$$n','El Proceso de anulacion se Realizo Exitosamente',information!)
dw_4.GetChild("iditem", dwc);dwc.reset()
gdc_mitemalma1.RowsCopy(gdc_mitemalma1.GetRow(), gdc_mitemalma1.RowCount(), Primary!, dwc, 1, Primary!)

iuo_grillamain.event ue_activa_opc( false)
this.event ue_cambio_opc('Consultar')
this.event ue_settabtext( 'Consultar')

dw_1.reset();dw_3.reset( )
iuo_grillamain.event dynamic ue_activa_opc(false) 
iuo_grillamain.event dynamic ue_bloquear_refrescar(false) 
this.event ue_setdwc()


//dw_4.GetChild("iditem", dwc);dwc.reset()
//gdc_mitem1.RowsCopy(gdc_mitem1.GetRow(), gdc_mitem1.RowCount(), Primary!, dwc, 1, Primary!)
//iuo_grillamain.event ue_activa_opc( false)
//this.event ue_cambio_opc('CONSULTAR')
////this.event ue_ver_object(true)
//this.event ue_settabtext( 'Consultar')
return 1
end event

event ue_imprimir;call super::ue_imprimir;int ll_iddocumento
//ll_iddocumento = dw_1.object
decimal {2} ld_cantPrint = 15.00

dw_5.dataobject  = 'd_usp_printdocuvta'
dw_5.settransobject( sqlca)
dw_5.retrieve(il_documento,ld_cantPrint)
dw_5.Object.DataWindow.Print.Paper.Size = 256
dw_5.Object.DataWindow.Print.CustomPage.Length = 140
dw_5.Object.DataWindow.Print.CustomPage.Width = 226  //7 inches
dw_5.print();commit;
//f_printmatricialdw_main(14,dw_5,'N')


end event

event ue_eliminar_detalle;integer li_fila
if dw_3.rowcount() = 0 then return
if f_msg('Esta seguro que desea eliminar este registro ??' + string(dw_3.getrow()), 2) = 2 then return 
dw_3.deleterow(0)
if dw_3.rowcount() = 0 then
	f_protegercolumna(80,dw_1,'idalma')	
	f_protegercolumna(1,dw_1,'idlista')
end if

end event

event ue_sumar;//integer li_fila
//double ld_preciobase,ld_totalDocu
//ld_totalDocu = 0 
//for li_fila = 1 to dw_3.rowcount( )
//	ld_preciobase = dw_3.Object.npreciobase[li_fila]
//	//dw_3.Object.nporc1[li_fila]=19 //igv
//	dw_3.Object.nPrecioImpuesto[li_fila]=ld_preciobase 
//	//total inc igv
//	dw_3.Object.ntotal[li_fila] = dw_3.Object.ncant[li_fila] * ld_preciobase
////	dw_3.Object.nneto[li_fila] = dw_3.Object.ntotal[li_fila] / 1.19
//	dw_3.Object.nneto[li_fila] = dw_3.Object.ntotal[li_fila] / (1 + (dw_3.Object.nporc1[li_fila]/100))
//	dw_3.Object.nimpuesto1[li_fila]=dw_3.Object.ntotal[li_fila] - dw_3.Object.nneto[li_fila]
//	
//	dw_3.Object.nprecioneto[li_fila]=dw_3.Object.nneto[li_fila] / dw_3.Object.ncant[li_fila]
//	
//	
//	ld_totalDocu = ld_totalDocu  + dw_3.Object.ntotal[li_fila]
//next
//
//
//dw_1.Object.ntotal[1] =ld_totalDocu

integer li_fila
double ld_preciobase,ld_totalDocu
ld_totalDocu = 0 
for li_fila = 1 to dw_3.rowcount( )
	ld_preciobase = dw_3.Object.npreciobase[li_fila]
	dw_3.Object.nPrecioImpuesto[li_fila]=ld_preciobase 
	dw_3.Object.ntotal[li_fila] = dw_3.Object.ncant[li_fila] * ld_preciobase
	IF dw_3.Object.nporc1[li_fila] = 0  THEN
		dw_3.Object.nneto[li_fila] =dw_3.Object.ntotal[li_fila] 
		dw_3.Object.nimpuesto1[li_fila]= 0
		dw_3.Object.nprecioneto[li_fila]=ld_preciobase
	ELSE
		
		dw_3.Object.nneto[li_fila] = dw_3.Object.ntotal[li_fila] / (1 + (dw_3.Object.nporc1[li_fila]/100))
		dw_3.Object.nimpuesto1[li_fila]=dw_3.Object.ntotal[li_fila] - dw_3.Object.nneto[li_fila]
		
		dw_3.Object.nprecioneto[li_fila]=dw_3.Object.nneto[li_fila] / dw_3.Object.ncant[li_fila]
		
		
	END IF
	ld_totalDocu = ld_totalDocu  + dw_3.Object.ntotal[li_fila]
	
next


dw_1.Object.ntotal[1] =ld_totalDocu
end event

event ue_parametros;integer li_param
s_parm 				lstr_parm
window w_to_open
istr_parm.auo_powerdock = uo_powerdock 
istr_parm.string_parm[1] = 'P'
li_param =istr_parm.integer_parm[2] 
//istr_parm.integer_parm[1] =1
//istr_parm.integer_parm[2] =1
//istr_parm.integer_parm[3] =9
OpenWithParm(w_to_open, istr_parm,is_window)	
lstr_parm=message.powerobjectparm
if lstr_parm.char_parm = '1' then
	istr_parm.integer_parm[1] =      lstr_parm.integer_parm[1]//centro o tienda
	istr_parm.integer_parm[2] =      lstr_parm.integer_parm[2]//almacen
	istr_parm.integer_parm[3] =      lstr_parm.integer_parm[3]//mes
	istr_parm.integer_parm[4] =      lstr_parm.integer_parm[4]//anio
	
//	dynamic of_setparam(lstr_parm.integer_parm[1],lstr_parm.integer_parm[2],lstr_parm.integer_parm[3])
	iuo_grillamain.dynamic of_setparam(lstr_parm.integer_parm[1],lstr_parm.integer_parm[2],lstr_parm.integer_parm[3],lstr_parm.integer_parm[4])
	iuo_grillamain.dynamic of_retrieve()
	event ue_ver_default( )
	//of_setparm( /*integer ai_parm1*/, /*integer ai_parm2*/, /*integer ai_parm3 */)
end if
end event

type dw_5 from u_cst_mantocabedet`dw_5 within u_cst_docuvta_factura_gr
end type

type dw_4 from u_cst_mantocabedet`dw_4 within u_cst_docuvta_factura_gr
string dataobject = "d_vtadocud_factura_tabular"
end type

type dw_3 from u_cst_mantocabedet`dw_3 within u_cst_docuvta_factura_gr
end type

event dw_3::itemchanged;call super::itemchanged;DataWindowChild 	LdwChild
string ls_descripcion, ls_orden, ls_artic
long ll_registro,ll_moneda,ll_exoigv,ll_return
decimal{4} ld_fm_precio 
decimal{2} ld_fm_dcto1 , ld_fm_dcto2 , ld_fm_dcto3 , ld_fm_dcto4
double ld_cant,ld_nstock,ld_porc1, ldprecioVenta
integer li_item	,li_alma
Choose Case dwo.name
	Case 'iditem'
		//-- Verifica si es valido
		If Trim(data) = '' Or IsNull(data) Then
			Messagebox('Sistema','Error, Digite un C$$HEX1$$f300$$ENDHEX$$digo Valido', StopSign!)
			Return 1
		End If
		li_item = integer (data)
		li_alma = Object.idalma[ row ]
		//-- Recupera las unidades de medida
		This.GetChild('idunid',LdwChild)
		LdwChild.SetTransObject(SQLCA)
		LdwChild.Retrieve(li_item,'V')
		if ldwChild.RowCount() <> 0 then
			Object.idunid[ row ] = ldwChild.GetItemString( 1 ,1)
		end if
		
		sqlca.usp_getstockmitemalma( li_item, li_alma, ld_nstock)
		Object.nstock[ row ] = ld_nstock
		
		ll_moneda = dw_1.object.idmoneda[1]
		ld_porc1 = 0 ;  ll_exoigv= 0; ldprecioVenta = 0; ll_return = 0
		sqlca.Usp_GetPrecioItem( li_item, 1,ll_moneda, ld_porc1, ll_exoigv,ldprecioVenta,ll_return)
		if ll_return > 0  then
			Object.npreciobase[ row ] = ldprecioVenta
			Object.nporc1[row] =ld_porc1
			Object.npreciobase[ row ] = ldprecioVenta
		End if
				
//		//-- Verifica si ya fue ingresado
//		If find('cc_servic=~''+data+'~'',1, RowCount()) >0 Then
//			Messagebox('Sistema','El Servicio ya Fue Registrado Anteriormente, Digite uno Valido', StopSign!)
//			Return 1
//		End If
Case 'idunid'
		//-- Si es valido
		If Trim(data) = '' Or IsNull(data) Then
			Messagebox('Sistema','Error, Digite un C$$HEX1$$f300$$ENDHEX$$digo Valido', StopSign!)
			Return 1
		End If

//		//-- Verifica si existe
//		ls_descripcion = Space(40)
//		SQLCA.sp_cat_mat_unidad_medida(data,ls_descripcion)
//		If Trim(ls_descripcion) = '' Then
//			Messagebox('Sistema','Error, C$$HEX1$$f300$$ENDHEX$$digo No Registrado, Digite uno Valido', StopSign!)
//			Return 1
//		End If
//
//		//-- Verifica las unidades de destino
//		ll_registro = 0
//		SQLCA.sp_alm_obt_aumalter(This.GetItemString(row,'cc_artic'),data,ll_registro)
//		If ll_registro = 0 Then
//			Messagebox('Sistema','Error, Unidad de Medida no Asignada al Art$$HEX1$$ed00$$ENDHEX$$culo, Digite Uno Valido', StopSign!)
//			Return 1
//		End If
	case 'ncant'
		if double (data)> 0 then post event ue_sumar()
	case 'npreciobase'
		post event ue_sumar()
//		  this.object.ntotal[row] = this.getitemDecimal( row,'ncant') *  double (data)
//	case ''
end choose
end event

event dw_3::ue_dropdowndw;call super::ue_dropdowndw;DataWindowChild 	LdwChild

This.GetChild('idunid',LdwChild)
LdwChild.SetTransObject(SQLCA)
LdwChild.InsertRow(0)

Return 1

end event

event dw_3::itemerror;call super::itemerror;Return 1
end event

event dw_3::rowfocuschanged;call super::rowfocuschanged;//-- Recupera las unidades de medida
If currentRow<=0 Then Return
integer li_item 
datawindowchild ldwc_1
this.getchild('idunid',ldwc_1);ldwc_1.reset()
ldwc_1.settransobject(sqlca)
li_item  = Object.iditem[currentrow]
if isnull(li_item ) then li_item =1
ldwc_1.Retrieve(li_item,'V')

end event

event dw_3::doubleclicked;call super::doubleclicked;integer li_sw
istr_parm.string_parm[1]=dwo.name
//istr_parm.integer_parm[1]=0

choose case dwo.name
	case 'iditem_1'
		if This.object.iditem.protect = '1' then return
		li_sw = 3
end choose

if li_sw > 0 then
	//w_m_notpedvta.event ue_proceso(istr_parm.integer_parm[1])
	of_buscar_dato( istr_parm.string_parm[1])
	
end if
end event

type st_det2 from u_cst_mantocabedet`st_det2 within u_cst_docuvta_factura_gr
end type

type st_det1 from u_cst_mantocabedet`st_det1 within u_cst_docuvta_factura_gr
end type

type st_titulo from u_cst_mantocabedet`st_titulo within u_cst_docuvta_factura_gr
integer width = 1842
end type

type dw_2 from u_cst_mantocabedet`dw_2 within u_cst_docuvta_factura_gr
string dataobject = "d_vtadocu_factura_free"
end type

type uo_toolbarstrip from u_cst_mantocabedet`uo_toolbarstrip within u_cst_docuvta_factura_gr
end type

event uo_toolbarstrip::ue_buttonclicked;string ls_columna,ls_tipo,ls_codigo
integer li_codigo
long ll_codigo,ll_opc


choose case upper(as_button)
	case 'IMPRIMIR'	
		il_documento = dw_2.object.data [1,1]
		event ue_print()
		update vtadocu set bimpreso = 1 where iddocumento = :il_documento;
		commit;
		dw_1.retrieve(il_documento)
		dw_2.object.data=dw_1.object.data
		commit;
		
		//event ue_copy_fila(dw_2, 1, 1)
	case 'IMPRIMIR-GRABAR'	
		ib_printGrabar = true
		event  ue_grabar( )
	case 'ANULAR'
		is_button = as_button
		event ue_anular()
	case 'ELIMINAR FILA'
		EVENT ue_eliminar_detalle()
	case 'AGREGAR'
		//FOR ll_codigo = 1 to 10000
			event ue_agregar()
		//next
	case 'EDITAR'
			is_button = as_button
			event ue_editar()
	case 'GRABAR'
		ib_printGrabar = false
		event  ue_grabar( )
	case 'CANCELAR'
		
	   if upper(is_button) ='NUEVO' then 
			is_button ='DEFAULT'
		ELSE
			is_button ='CANCELAR'
	   end if	
	  // is_button = as_button
       event ue_cancelar()

END CHOOSE
end event

event uo_toolbarstrip::constructor;this.of_AddItem('Grabar', 'Save!')
this.of_AddItem('Imprimir-Grabar', 'Print!')
this.of_AddItem('Editar', '..\IMG\ICO\edit.ico')
this.of_AddItem('Anular', '..\IMG\ICO\Error.ico')
this.of_AddItem('Agregar', 'Insert5!')
this.of_AddItem('Eliminar Fila', 'DeleteRow!')
this.of_AddItem('Cancelar', 'Undo!')
this.of_AddItem('Imprimir', 'Print!')


//opciones en estado invisible
this.of_setvisible( 'Grabar', false)
this.of_setvisible( 'Agregar', false)
this.of_setvisible( 'Eliminar Fila', false)
this.of_setvisible( 'Cancelar', false)
this.of_setvisible('Imprimir-Grabar', false)
this.of_setvisible('Imprimir', false)
//esto es para mostar los textos
this.of_DisplayText(true)



end event

type dw_1 from u_cst_mantocabedet`dw_1 within u_cst_docuvta_factura_gr
end type

event dw_1::buttonclicking;call super::buttonclicking;choose case dwo.name
	case 'b_cliente'
		SetPointer(HourGlass!)
		dw_1.GetChild("idclie", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )
		commit;
		SetPointer(Arrow!)
end choose
end event

type uo_1 from u_cst_mantocabedet`uo_1 within u_cst_docuvta_factura_gr
end type

